//
//  RFDetailListTableViewController.m
//  RadioFree
//
//  Created by zhaojianguo-PC on 14-5-23.
//  Copyright (c) 2014年 xiaozi. All rights reserved.
//

#import "RFDetailListViewController.h"
#import "UIView+Additions.h"
#import "StyledTableViewCell.h"
#import "RFRadioView.h"
#import "FSPlaylistItem.h"

@interface RFDetailListViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    UITableView * _tableView;
    RFRadioView * _radioView;
    UILabel * titleLabel;
}
@property(nonatomic, strong) UIButton *button;

@end

@implementation RFDetailListViewController
@synthesize array = _array;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:232.f/255.f green:184.f/255.f blue:10.f/255.f alpha:1.f];
    UIImage *hamburger = [UIImage imageNamed:@"hamburger"];
    NSParameterAssert(hamburger);
    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
    self.button.frame = CGRectMake(10.0f, 20.0f, 40.0f, 40.0f);
    [self.button setImage:hamburger forState:UIControlStateNormal];
    [self.button addTarget:self action:@selector(openDrawer:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.button];
    
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.view.size.width/2-150/2, 25, 150, 30)];
    titleLabel.backgroundColor = [UIColor clearColor];
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:18.0f];
    titleLabel.text = @"RADIO FREE";
    [self.view addSubview:titleLabel];
    
    _tableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, self.button.origin.y+self.button.size.height+5, self.view.size.width, self.view.size.height-self.button.size.height-self.button.origin.y-105)];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    
    _radioView = [[RFRadioView alloc] initWithFrame:CGRectMake(0, _tableView.origin.y+_tableView.size.height, _tableView.size.width,100)];
    [self.view addSubview:_radioView];
    

    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
-(void)setArray:(NSArray *)array_
{
    if (_array !=array_) {
        _array = array_;
        [_tableView reloadData];
    }
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
//#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return [_array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * string = @"Cell";
    StyledTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:string];
    if (cell == nil) {
        cell = [[StyledTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:string];
    }
    // Configure the cell...
    NSDictionary * dictionary = [_array objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [dictionary objectForKey:@"Name"];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary * dictionary = [_array objectAtIndex:indexPath.row];
    NSString * name = [dictionary objectForKey:@"Name"];
     NSString * url = [dictionary objectForKey:@"Pls"];
    FSPlaylistItem * item = [[FSPlaylistItem alloc] init];
    item.title = name;
    item.url = url;
    _radioView.selectedPlaylistItem = item;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Configuring the view’s layout behavior

- (BOOL)prefersStatusBarHidden
{
    return NO;
}

- (UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleLightContent;
}

#pragma mark - ICSDrawerControllerPresenting

- (void)drawerControllerWillOpen:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = NO;
}

- (void)drawerControllerDidClose:(ICSDrawerController *)drawerController
{
    self.view.userInteractionEnabled = YES;
}
#pragma mark - Open drawer button

- (void)openDrawer:(id)sender
{
    [self.drawer open];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
